﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Notoric.Model.Validation
{
    /// <summary>
    /// Implements a simple rule that can perform the validation via a delegate.
    /// </summary>
    /// <remarks>
    /// This class was implemented because most of the validation rules are very simple
    /// and can be realized by just one line of code.
    /// </remarks>
    public class SimpleRule : Rule
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="SimpleRule"/> instance.
        /// </summary>
        /// <param name="propertyName">
        /// <para>
        /// Type: <see cref="System.String"/>
        /// </para>
        /// <para>
        /// Name of the property controlled by this rule.
        /// </para>
        /// </param>
        /// <param name="description">
        /// <para>
        /// Type: <see cref="System.String"/>
        /// </para>
        /// <para>
        /// Description of this rule.
        /// </para>
        /// </param>
        /// <param name="descriptionKey">
        /// <para>
        /// Type: <see cref="System.String"/>
        /// </para>
        /// <para>
        /// Dictionary key of the rule description.
        /// </para>
        /// </param>
        /// <param name="validator">
        /// <para>
        /// Type: <see cref="SimpleRuleDelegate"/>
        /// </para>
        /// <para>
        /// Method that will perform the validation
        /// </para>
        /// </param>
        public SimpleRule(string propertyName, string description, string descriptionKey, SimpleRuleDelegate validator)
            : base(propertyName, description, descriptionKey)
        {
            this.validator = validator;
        }

        #endregion

        #region Rule Members

        /// <inheritdoc/>
        public override bool ValidateValue(object value)
        {
            return validator(value);
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds a reference to the delegate that will do the validation.
        /// </summary>
        private SimpleRuleDelegate validator;

        #endregion
    }

    /// <summary>
    /// Represents a method that will perform the validation for a <see cref="SimpleRule"/>.
    /// </summary>
    /// <param name="value">
    /// <para>
    /// Type: <see cref="System.Object"/>
    /// </para>
    /// <para>
    /// Validated value
    /// </para>
    /// </param>
    /// <returns>
    /// <para>
    /// Type: <see cref="System.Boolean"/>
    /// </para>
    /// <para>
    /// Validation Result:
    /// <see langword="true"/> if the validation succeeds, <see langword="false"/> otherwise.
    /// </para>
    /// </returns>
    public delegate bool SimpleRuleDelegate(object value);
}
